
* This file uses data from the SOM voter surveys to produce the results
* for voters in Appendix Figur W1 and Table W1. It also produces the validation 
* test of the social class categorization in Appendix Table W4

* The data was requested from www.snd.se, the Swedish depository for research data
* We are using the cumulative SOM survey data for 1986-2020.

use "C:\Users\Johanna\Dropbox\Worker rep\SOM-undersökningar\SND0905-001-V10 Den nationella SOM-undersökningens kumulativa dataset\Super-R-SOM (1986-2020) v2021.1.dta", clear

*********************
* Cleaning
*********************

drop if age<18

* Create demographic variables
gen woman = 1 if sex==1
replace woman = 0 if sex==2

gen student = 1 if lmsit==6
replace student = 0 if student==. & lmsit!=.

gen pens = 1 if age>65 & age!=. /* retirees */
replace pens = 0 if age<65


****************************************
* Defining workers
****************************************

* 1) self-identification
* students = 0, retired self-identify
gen workerselfid=1 if occgr==4 | occgr==5
replace workerselfid=0 if workerselfid==. & occgr<12 & occgr!=.
* occgr 12 and 13 are "no response" or "double marks"

* Set students to non-workers
replace workerselfid=0 if stud==1
tab workerselfid

* 2) Occupation-based  

replace ssyk = . if ssyk==9999
tostring ssyk, gen(ssykstr)
gen ssyk96 = substr(ssykstr,1,3)
destring ssyk96, replace force

* Merge with data for occupation code (dataset available in Dataverse)
joinby ssyk96 using "C:\Users\Johanna\Documents\SOM analys workers\class from ssyk.dta", unmatched(master)

tab sector
count if sector==3
gen private=1 if sector==3
replace private=0 if sector ==1
replace private=0 if sector ==2

* Merge with data for occupation code and sector (dataset available in Dataverse)
drop _merge
joinby ssyk96 privat using "C:\Users\Johanna\Documents\SOM analys workers\ssyk3_priv_sei.dta", unmatched(master)
bysort year: tab sei /* available in 2005-2020 */



********************************************************************
** Verify with self-identification among working-age non-students **
********************************************************************

* # of observations
count if workerselfid!=. & year>2004 
count if workerselfid!=. & year>2004 & sei!=.

* comparison point in the population
tab workerselfid if year>2004 /* 38.5% */
tab workerselfid if year>2004 & sei!=. /* 39.3% */
* very similar in sample with non-missing SEI

* Our definition based on SEI
gen workersei = 1 if sei==11 | sei==21
replace workersei = 0 if workersei==. & sei!=.

* proportion of workers with sei definition
tab workersei /* 38.9% */
tab workersei if pens!=1 & stud!=1 /* 38.1% */

*********************************************************************
* Table A4. Self-identified workers by occupation-based class
*********************************************************************

tab workerself sei, column

* Proportions of errors
gen errorsei = 1 if workerself==1 & workersei==0
replace errorsei = 1 if workerself==0 & workersei==1
replace errorsei=0 if workerself==1 & workersei==1
replace errorsei=0 if workerself==0 & workersei==0
tab errorsei if year>2004 /* 19.8% */


*************************
*** Workers' ideology ***
*************************

* Restrict sample to main parties
ren cb10 party
keep if party<8 | party ==10

* Class
gen class = sei
replace class = 11 if class==21
tab class

recode class (11=1)(36=2)(46=3)(56=4)(79=5)(89=6)
tab class

* Ideology 
* Left-right ideology 
tab ba10
replace ba10 = . if ba10>5
destring ba10, replace force
ren ba10 leftright
egen s_leftright = std(leftright)

* Cut public sector
tab ea900a
gen cutpublic = ea900a
recode cutpublic (1=5)(2=4)(4=2)(5=1)(3=3)
replace cutpublic=. if cutpublic>5
tab cutpublic
egen s_cutpublic = std(cutpublic)


cd "C:\Users\Johanna\Dropbox\Worker rep\kolfu som analysis"

* cleaning
* number of observations
tab year if s_leftright!=. & class!=. /* available from 2005-2020 */
tab year if s_cutpublic!=. & class!=. /* available from 2005-2013 */

****************************************************************
* Figure W1. Results for voters 
****************************************************************

foreach var in s_leftright s_cutpublic {
preserve

collapse (mean) mean`var'= `var' (sd) sd`var'=`var' (count) n=`var', by(class)
generate hi`var' = mean`var' + invttail(n-1,0.025)*(sd`var' / sqrt(n))
generate low`var' = mean`var' - invttail(n-1,0.025)*(sd`var' / sqrt(n))

twoway (bar mean`var' class, barwidth(0.6) bcolor(gray)) ///
(rcap hi`var' low`var' class, lcolor(black)), ///
graphregion(color(white)) ylabel(, angle(0)) legend(off) ///
ysize(3) xsize(6) scale(1.2) xtitle("") title(`var', size(medium)) ///
yscale(range(-0.3 0.8)) ylabel(-0.2 0 0.2 0.4 0.6 0.8)

graph save `var'workersom.gph, replace

restore
}

graph combine s_leftrightworkersom.gph s_cutpublicworkersom.gph, ///
 col(1) graphregion(color(white)) ysize(7)


******************************************************************
* Table W1. Regression results for political ideology and
* preferences by social class
******************************************************************

* Control variables

* Birth region control
tab growupp
gen sweden = 1 if growupp<5
replace sweden = 0 if growupp>4 & growupp!=.

gen valar = 1986 if year==1986 | year==1987 
replace valar = 1988 if year==1988 | year==1989 | valar==1990
replace valar = 1991 if year==1991 | year==1992 | valar==1992
replace valar = 1994 if year==1994 | year==1995 | valar==1996 | valar==1997
replace valar = 1998 if year==1998 | year==1999 | valar==2000 | valar==2001
replace valar = 2002 if year==2002 | year==2003 | valar==2004 | valar==2005
replace valar = 2006 if year==2006 | year==2007 | valar==2008 | valar==2009
replace valar = 2010 if year==2010 | year==2011 | valar==2012 | valar==2013
replace valar = 2014 if year==2014 | year==2015 | valar==2016 | valar==2017
replace valar = 2018 if year==2018 | year==2019 | valar==2020 

* Regressions

reg  s_leftright i.class
outreg2 using classpref, excel dec(2) replace

areg  s_leftright i.class i.sex i.sweden i.age4a i.valar i.edu3, abs(kommun)
outreg2 using classpref, excel dec(2) append

areg  s_leftright i.class i.sex i.sweden i.age4a i.valar i.edu3 i.party, abs(kommun)
outreg2 using classpref, excel dec(2) append

reg  s_cutpublic i.class
outreg2 using classpref, excel dec(2) append

areg  s_cutpublic i.class i.sex i.sweden i.age4a i.valar i.edu3, abs(kommun)
outreg2 using classpref, excel dec(2) append

areg  s_cutpublic i.class i.sex i.sweden i.age4a i.valar i.edu3 i.party, abs(kommun)
outreg2 using classpref, excel dec(2) append

